<template>
  <div>
    <el-card>
      <el-tabs v-model="activeName">
        <el-tab-pane label="登录账号设置" name="first" style="margin-top:40px">
          <el-form ref="ruleForm" :model="ruleForm" :rules="rules">
            <el-form-item label="姓名" prop="username" label-width="120px">
              <el-input v-model="ruleForm.username" style="width:400px" placeholder="请输入姓名" />
            </el-form-item>
            <el-form-item label="密码" prop="password" label-width="120px">
              <el-input v-model="ruleForm.password2" style="width:400px" type="password" placeholder="请输入密码" />
            </el-form-item>
            <el-form-item label="手机号码" label-width="120px">
              <el-input v-model="ruleForm.mobile" style="width:400px" placeholder="请输入手机号" />
            </el-form-item>
            <el-form-item label-width="120px">
              <el-button type="primary" @click="saveUser" >更新</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="个人详情" name="second"><personal-detail/></el-tab-pane>
        <el-tab-pane label="岗位信息" name="third"><jobInfo /></el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>
<script>
import { getUserDetailById, saveUserDetailById } from '@/api/employees';
import jobInfo from './components/jobInfo'
import personalDetail from './components/personalDetail'
export default {
  components:{jobInfo,personalDetail},
  data() {
    return {
      ruleForm: {
        username:'',
        password2:'',
        mobile:''  
      },
      activeName: 'first',
      userId: '',
      rules: {
        username: [
          { required: true, message: '请输入姓名', trigger: 'change' },
          { min: 2, max: 5, message: '长度在 2 到 5个字符', trigger: 'blur' }
        ],
        password2: [
          { required: true, message: '新密码不能为空', trigger: 'change' },
          { min: 6, max: 16, message: '密码长度在6~16位', trigger: 'blur' }
        ]
      }

    }
  },
  created() {
    this.userId = this.$route.params.id // 点击操作时，获取每个用户不同的id
    console.log(this.$route.params.id)
    this.getUserDetailById()
  },
  methods: {
    async getUserDetailById() {
      this.ruleForm = await getUserDetailById(this.userId)
    },
    async saveUser(){
      try{
        await this.$refs.ruleForm.validate()
        await saveUserDetailById({...this.ruleForm, password:this.ruleForm.password2})
        this.$message.success("更新密码成功")
      }catch(error){
        console.log(error)
      }
    }

  }

}
</script>
